Ext.ns('TaxonTagger');
TaxonTagger.mainViewport = function(config){
	
	this.taggerpanel = new TaxonTagger.taggerpanel({
		listeners: {
				switchView: function(mode){
					this.tbar.btnView.menu.btnTaxonMode.setChecked(true);
					this.modeChanged(mode);
				}
			,	urlLoaded: function(url){
					this.htw.loadMask.show();
					this.htw.urlCombo.setValue(url);
					this.htw.lasturlLoaded = url;
				}
			,	tabDisable: function(){
					this.tabpanel.disable();
				}	
			,	docDataChanged: TaxonTagger.saveStorage
			,	scope: this
		}
	});

	this.tabpanel = new TaxonTagger.tabpanel({
			region: 'east'		
		,	split: true
		,	disabled: false
		,	listeners: {
				render: function(){
					this.disable();
				}
			}
	});
	
	this.maincard = new Ext.Panel({
			layout: 'border'
		,	border: false
		,	defaults: {
				border: false
			}
		,	items: [this.taggerpanel, this.tabpanel]	
	})
	
	this.htw = new TaxonTagger.NameGrid();
	
	this.htw.on('switchView', function(mode){
		this.tbar.btnView.menu.btnDocumentMode.setChecked(true);
		this.modeChanged(mode);
	}, this);
	
	this.htw.on('urlLoaded', function(url){
		this.taggerpanel.urlCombo.setValue(url);
		this.taggerpanel.loadDocument(url);
		this.taggerpanel.lasturlLoaded = url;
	}, this);
	
	this.taggerpanel.on("documentloaded", function(frame) {
		frame.unmask();
		this.addIframeListeners(frame);
	});

	this.taggerpanel.on("afterDocumentLoaded", TaxonTagger.loadStorage, this.tabpanel );
	
	this.taggerpanel.on("removeName", function(o, sciname, txt){
		var f = this.taggerpanel.getFrameDocument().body;
		var parts = sciname.split(" "); 
		if (parts.length > 1) {
			parts[0] = parts[0][0] + ".";
		}
		abbName = parts.join(" ");
		Ext.each(Ext.fly(f).select("span.tt-remove").elements, function(item) {
			var o2 = Ext.fly( this );
			if(o2.prev() == null) {
/*				
				Ext.DomHelper.insertBefore(clickEl, txt);
				o.prev().remove();
				o.remove();
*/				
				return;	
			} else if (o2.prev().dom.getAttribute("scientificName") == txt) {
				if (o2.prev().dom.getAttribute("verbatim") == abbName) {
					Ext.DomHelper.insertBefore(o2.prev(), abbName);
				} else {
					Ext.DomHelper.insertBefore(o2.prev(), txt);
				}
				o2.prev().remove();
				o2.remove();
			} else if (o2.prev().dom.getAttribute("scientificName") == sciname) {
				if (o2.prev().dom.getAttribute("verbatim") == txt) {
					Ext.DomHelper.insertBefore(o2.prev(), txt);
				} else {
					Ext.DomHelper.insertBefore(o2.prev(), o2.prev().dom.getAttribute("scientificName"));
				}
				o2.prev().remove();
				o2.remove();
			}

			Ext.each(TaxonTagger.currentDocument.customWords, function(item){
				if(item == txt || item == sciname){
					TaxonTagger.currentDocument.customWords.remove(item);
					TaxonTagger.saveStorage();
				}
			}, this);

			for (key in TaxonTagger.currentDocument.comments) {
				if(key == sciname){
					delete TaxonTagger.currentDocument.comments[key];
					TaxonTagger.saveStorage();
				}
			}
		});

		TaxonTagger.currentDocument.removedWords.push(sciname);
		TaxonTagger.saveStorage();
		this.taggerpanel.updateLocalStorage();
		var fId = TaxonTagger.docStore.findExact('scientificName', sciname);
		if(fId != -1){
			TaxonTagger.docStore.removeAt(fId);
		}
		this.taggerpanel.generateList();
		TaxonTagger.docStore.reload();
	}, this);
	
	this.taggerpanel.resetLocal.on("click", function(){
		var url = this.taggerpanel.lasturlLoaded;
		localStorage.removeItem('url-'+url);
		this.taggerpanel.lasturlLoaded='';
		TaxonTagger.currentDocument = TaxonTagger.defaultDocumentSettings;
		TaxonTagger.currentDocument.info.url = url;
		TaxonTagger.saveStorage();
		this.taggerpanel.loadDocument(url);		
	},this);

	this.taggerpanel.webLink.on("click", function(){
		window.open(this.taggerpanel.urlCombo.getValue(), '_blank');
	},this);

	this.tabpanel.docinfoPanel.on("docDataChanged", TaxonTagger.saveStorage);
	
	this.tbar = new TaxonTagger.toolbar({
		listeners: {
				modeChanged: this.modeChanged
			,	scope: this
		}
	});

	var tConfig = Ext.decode(localStorage.getItem("settings")) || Config;
	Ext.apply(this, config, {
			layout: 'fit'
		,	id: 'viewport'
		,	items: [{
					region: 'center'
				,	border: false
				,	title: this.viewportTitle + '(' + tConfig["general"]["nfServerName"] + ')'
				,	layout: 'card'
				,	ref: 'centerPanel'
				,	scope: this
				,	tbar: this.tbar
				,	activeItem: 0
				,	defaults: {
						border: false
					}
				,	items: [this.maincard, this.htw]
			}]
	});

	TaxonTagger.mainViewport.superclass.constructor.call(this, config);
	
};

Ext.extend(TaxonTagger.mainViewport, Ext.Viewport, {

		modeChanged: function(mode){
			if (mode == 'documentMode'){
				this.centerPanel.getLayout().setActiveItem(0);
				TaxonTagger.docStore.load({params:{start:0, limit: 30}});
			} else {
				this.centerPanel.getLayout().setActiveItem(1);
				var myPageSize = 30;
				if (TaxonTagger.docStore.allData) {
					myPageSize = TaxonTagger.docStore.allData.length;
				}
				TaxonTagger.docStore.load({params:{start:0, limit: myPageSize}});
			}
		}

});